package org.example.repository;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.MDC;

import java.util.concurrent.atomic.AtomicInteger;

@Aspect
public class TestAspect {

    private static final String TEST_ID = "testId";

    @Pointcut("@annotation(Test)")
    public void test() {
    }

    @Before("test()")
    public void beforeTest(JoinPoint joinPoint) {
        MDC.put(TEST_ID, nextId());
    }

    @After("test()")
    public void afterTest(JoinPoint joinPoint) {
        MDC.remove(TEST_ID);
    }

    private static final AtomicInteger seed = new AtomicInteger();

    private static String nextId() {
        return "test-" + seed.incrementAndGet();
    }
}
